﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using DefaultScope;
using System.Collections.ObjectModel;

namespace Test
{
    public partial class LoginPage : PhoneApplicationPage
    {
        DefaultScopeOfflineContext context;
        
        public LoginPage()
        {
            context = new DefaultScopeOfflineContext("list_client", new Uri("http://localhost:1192/DefaultScopeSyncService.svc/"));

            //context.CacheController.ControllerBehavior.SerializationFormat = Microsoft.Synchronization.ClientServices.SerializationFormat.ODataAtom;
            
            context.LoadCompleted += new EventHandler<Microsoft.Synchronization.ClientServices.IsolatedStorage.LoadCompletedEventArgs>(context_LoadCompleted);

            this.Loaded += new RoutedEventHandler(LoginPage_Loaded);

            context.CacheController.RefreshCompleted += (sender, args) =>
            {
                if (args.Error != null)
                {
                    Dispatcher.BeginInvoke(
                        delegate
                        {
                            MessageBox.Show(args.Error.ToString());
                        });
                }
            };
            
            InitializeComponent();
        }

        void context_LoadCompleted(object sender, Microsoft.Synchronization.ClientServices.IsolatedStorage.LoadCompletedEventArgs e)
        {
            Dispatcher.BeginInvoke(delegate
            {
                employeesList.ItemsSource = context.CustomersCollection;
            });
        }

        void LoginPage_Loaded(object sender, RoutedEventArgs e)
        {
            context.LoadAsync();
        }
  
   
        private void loginBtn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                context.SaveChanges();
                context.CacheController.RefreshAsync();
            }
            catch (Exception ee)
            {
                Dispatcher.BeginInvoke(delegate
                {
                    MessageBox.Show(ee.ToString());
                });
            }
            /*
             * Employees emp = (Employees)employeesList.SelectedItem;
            MessageBox.Show("Hello " +  emp.ToString());
            NavigationService.Navigate(new Uri("/MainPage.xaml?employeeID=" + emp.EmployeeID, UriKind.Relative));
             * */
        }
    }
}